package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-29 02:24
 */
public class RegionsBySlashes {

    public int regionsBySlashes(String[] grid) {

        int N = grid.length;
        UF uf = new UF(4 * N * N);
        for (int i = 0; i < N; i++) {
            char[] chars = grid[i].toCharArray();
            for (int j = 0; j < N; j++) {
                int index = 4 * (i * N + j);
                if (chars[j] == ' ') {
                    uf.union(index, index + 1);
                    uf.union(index + 1, index + 2);
                    uf.union(index + 2, index + 3);
                }
                if (chars[j] == '\\') {
                    uf.union(index, index + 1);
                    uf.union(index + 2, index + 3);
                }
                if (chars[j] == '/') {
                    uf.union(index, index + 3);
                    uf.union(index + 1, index + 2);
                }
                if (i < N - 1) uf.union(index + 2, 4 * ((i + 1) * N + j));
                if (j < N - 1) uf.union(index + 1, 4 * (i * N + j + 1)+3);
            }
        }
        return uf.count();
    }
}
